package com.xintai.approval.controller;

import java.util.List;

import com.xintai.approval.domain.FlowCaigouApply;
import com.xintai.approval.domain.FlowWorkflow;
import com.xintai.approval.service.IFlowCaigouApplyService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.xintai.common.annotation.Log;
import com.xintai.common.core.controller.BaseController;
import com.xintai.common.core.domain.AjaxResult;
import com.xintai.common.enums.BusinessType;
import com.xintai.approval.domain.FlowApply;
import com.xintai.approval.service.IFlowApplyService;
import com.xintai.common.utils.poi.ExcelUtil;
import com.xintai.common.core.page.TableDataInfo;

/**
 * 申请Controller
 *
 * @author xintai
 * @date 2021-02-01
 */
@RestController
@RequestMapping("/approval/apply")
public class FlowApplyController extends BaseController
{
    @Autowired
    private IFlowApplyService flowApplyService;
    @Autowired
    private IFlowCaigouApplyService flowCaigouApplyService;

    /**
     * 查询申请列表
     */
    @PreAuthorize("@ss.hasPermi('approval:apply:list')")
    @GetMapping("/list")
    public TableDataInfo list(FlowApply flowApply)
    {
        startPage();
        List<FlowApply> list = flowApplyService.selectFlowApplyList(flowApply);
        return getDataTable(list);
    }

    /**
     * 导出申请列表
     */
    @PreAuthorize("@ss.hasPermi('approval:apply:export')")
    @Log(title = "申请", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(FlowApply flowApply)
    {
        List<FlowApply> list = flowApplyService.selectFlowApplyList(flowApply);
        ExcelUtil<FlowApply> util = new ExcelUtil<FlowApply>(FlowApply.class);
        return util.exportExcel(list, "apply");
    }

    /**
     * 获取申请详细信息
     */
    @PreAuthorize("@ss.hasPermi('approval:apply:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(flowApplyService.selectFlowApplyById(id));
    }

    /**
     * 新增申请
     */
    @PreAuthorize("@ss.hasPermi('approval:apply:add')")
    @Log(title = "申请", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FlowApply flowApply)
    {
        Long i = flowCaigouApplyService.insertFlowCaigouApply(flowApply.getFlowCaiGouApply());
        flowApply.setDetailId(i);
        return toAjax(flowApplyService.insertFlowApply(flowApply));
    }

    /**
     * 修改申请
     */
    @PreAuthorize("@ss.hasPermi('approval:apply:edit')")
    @Log(title = "申请", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FlowApply flowApply)
    {
        return toAjax(flowApplyService.updateFlowApply(flowApply));
    }

    /**
     * 删除申请
     */
    @PreAuthorize("@ss.hasPermi('approval:apply:remove')")
    @Log(title = "申请", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(flowApplyService.deleteFlowApplyByIds(ids));
    }

    /**
     * 发布审批
     */
    @RequestMapping("/start")
    public AjaxResult apply(@RequestBody FlowApply flowApply){
        return toAjax(flowApplyService.apply(flowApply)) ;
    }

    /**
     * 进程
     * @return
     */
    @RequestMapping("/course")
    public TableDataInfo course(Long id){
        List<FlowWorkflow> course = flowApplyService.course(id);
        return getDataTable(course);
    }

}
